<template>
  <div>
    <el-card class="box-card" shadow="hover">
      <Pagination ref="pagination" :queryPage="loadPage">
        <div slot="search">
          <el-form :inline="true" :model="queryForm" size="mini">
            <el-form-item label="主机名称">
              <el-input v-model="queryForm.name" placeholder="名称"/>
            </el-form-item>
            <el-form-item label="主机">
              <el-input v-model="queryForm.host" placeholder="主机"/>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="$refs.pagination.go(1);">查询</el-button>
              <el-button type="success" @click="onEdit({})">新增</el-button>
              <el-button type="warning" @click="onImport()">批量导入</el-button>
            </el-form-item>
          </el-form>
        </div>
        <el-table slot="table" :data="pageData" border size="mini" style="width: 100%">
          <el-table-column label="#ID" prop="id" width="80"/>
          <el-table-column label="名称" prop="name" width="150"/>
          <el-table-column label="主机" prop="host" width="150"/>
          <el-table-column label="系统名称" prop="osName" width="100"/>
          <el-table-column label="系统版本" prop="osVersion" width="100"/>
          <el-table-column label="说明" prop="remark"/>
          <el-table-column label="创建时间" prop="created" width="150"/>
          <el-table-column label="更新时间" prop="updated" width="150"/>
          <el-table-column fixed="right" label="操作" width="150">
            <template slot-scope="scope">
              <el-button size="small" type="text" @click="onEdit(scope.row)">编辑</el-button>
              <el-button size="small" type="text" @click="onFileDrawer(scope.row.id)">目录</el-button>
              <el-button size="small" type="text" @click="onConnectDrawer(scope.row.id)">连通</el-button>
            </template>
          </el-table-column>
        </el-table>
      </Pagination>
    </el-card>

    <el-dialog :visible.sync="editVisible" title="主机设置" width="600px">
      <el-form :model="editData" label-width="80px" size="mini" style="width: 400px;margin-left: 50px">
        <el-form-item label="名称">
          <el-input v-model="editData.name"/>
        </el-form-item>
        <el-form-item label="主机">
          <el-input v-model="editData.host" :disabled="editData.id!=null"></el-input>
        </el-form-item>
        <el-form-item label="说明">
          <el-input v-model="editData.remark" :rows="2" type="textarea"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSave">保存</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <el-dialog :visible.sync="importVisible" title="批量导入" width="600px">
      <el-upload :action="uploadUrl" :before-upload="beforeUpload" :headers="uploadHeaders" :on-error="uploadError"
                 :on-success="uploadSuccess" :show-file-list="false" drag>
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div slot="tip" class="el-upload__tip">
          <a href="../template/hostDemo.csv">模版下载</a>
          <br>只能上传csv文件，且不超过500kb
        </div>
      </el-upload>
    </el-dialog>


    <el-dialog :visible.sync="connectFormVisible" title="主机连通" width="600px">
      <el-form :model="connectForm" label-width="80px" size="mini" style="width: 400px;margin-left: 50px">
        <el-form-item label="连通方式">
          <el-radio-group v-model="connectForm.type">
            <el-radio-button :label="1">免登</el-radio-button>
            <el-radio-button :label="2">用户名</el-radio-button>
            <el-radio-button :label="3">密钥</el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-if="connectForm.type==2" label="用户名">
          <el-input v-model="connectForm.username"/>
        </el-form-item>
        <el-form-item v-if="connectForm.type==2" label="密码">
          <el-input v-model="connectForm.password" show-password/>
        </el-form-item>
        <el-form-item v-if="connectForm.type==3" label="密钥">
          <el-input v-model="connectForm.identity" :rows="8" type="textarea"/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onConnectHost">连通</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <FileTreeDrawer ref="fileTreeDrawer" :download="downloadFile" :load="loadFileList" title="文件目录"/>

  </div>
</template>
<script>
import {
  downloadSshFileApi,
  hostInstancePageApi,
  saveHostPageApi,
  sshFileListApi,
  testSshConnectApi
} from "@/api/manage";
import Pagination from "@/components/Pagination";
import FileTreeDrawer from "@/components/FileTreeDrawer";

export default {
  data() {
    return {
      queryForm: {},
      connectForm: {},
      pageData: [],
      editData: {},
      uploadUrl: `${process.env.VUE_APP_BASE_API}manage/host/import`,
      uploadHeaders: {},
      importVisible: false,
      editVisible: false,
      connectFormVisible: false,
      fileDrawerHostId: null,
    };
  },
  components: {Pagination, FileTreeDrawer},
  methods: {
    async loadPage(current, size) {
      let query = Object.assign({current: current, size: size,}, this.queryForm);
      let data = await hostInstancePageApi(query);
      this.pageData = data.records;
      return data;
    },
    onEdit(data) {
      this.editData = Object.assign({}, data);
      this.editVisible = true;
    },
    onImport() {
      this.importVisible = true;
    },
    loadFileList(dir) {
      return sshFileListApi(this.fileDrawerHostId, dir);
    },
    downloadFile(fileName) {
      return downloadSshFileApi(this.fileDrawerHostId, fileName);
    },
    async sshFileList(hostId, dir) {
      let list = await sshFileListApi(hostId, dir);
      for (let i in list) {
        list[i].hasChildren = (list[i].fileType == 1);
      }
      return list;
    },
    onFileDrawer(hostId) {
      this.fileDrawerHostId = hostId;
      this.$refs.fileTreeDrawer.openDrawer("/");
    },
    async onSave() {
      await saveHostPageApi(this.editData);
      this.$message({type: "success", message: "保存成功!"});
      this.editVisible = false;
      this.$refs.pagination.go(1);
    },
    beforeUpload() {
      this.uploadHeaders["x-token"] = localStorage.getItem("login_token");
    },
    uploadSuccess() {
      this.$message({type: "success", message: "导入成功!"});
      this.importVisible = false;
      this.$refs.pagination.go(1);
    },
    uploadError(err) {
      let res = JSON.parse(err.message)
      this.$message.error("导入失败:" + res.errMsg);
    },
    onConnectDrawer(hostId) {
      this.connectForm = {hostId: hostId};
      this.connectFormVisible = true;
    },
    async onConnectHost() {
      await testSshConnectApi(this.connectForm);
      this.$message({type: "success", message: "连通成功!"});
      this.$refs.pagination.go();
      this.connectFormVisible = false;
    }
  },
};
</script>
